import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:toastification/toastification.dart';

class CuToast {
  // 初始化 EasyLoading 配置
  static void initEasyLoading() {
    EasyLoading.instance
      ..displayDuration = const Duration(milliseconds: 2000)
      ..indicatorType = EasyLoadingIndicatorType.fadingCircle
      ..loadingStyle = EasyLoadingStyle.dark
      ..indicatorSize = 45.0
      ..radius = 10.0
      ..progressColor = Colors.yellow
      ..backgroundColor = Colors.green
      ..indicatorColor = Colors.yellow
      ..textColor = Colors.yellow
      ..toastPosition = EasyLoadingToastPosition.bottom
      ..maskColor = Colors.blue.withAlpha(128)
      ..userInteractions = true
      ..dismissOnTap = false;
  }

  // 封装通用的 toast 显示方法
  static void _showToast(
    BuildContext context,
    String title,
    String msg,
    ToastificationType type,
  ) {
    toastification.show(
      context: context,
      title: Text(title),
      style: ToastificationStyle.flat,
      type: type,
      alignment: Alignment.topCenter,
      description: Text(msg),
      borderRadius: BorderRadius.circular(12),
      showProgressBar: false,
      autoCloseDuration: const Duration(seconds: 3),
    );
  }

  static success(BuildContext context, String title, String msg) {
    _showToast(context, title, msg, ToastificationType.success);
  }

  static error(BuildContext context, String title, String msg) {
    _showToast(context, title, msg, ToastificationType.error);
  }

  // 通知提示
  static toast(String msg,
      {EasyLoadingToastPosition toastPosition =
          EasyLoadingToastPosition.bottom}) {
    EasyLoading.instance.toastPosition = toastPosition;
    EasyLoading.showToast(msg);
  }
}
